/**
 * @filename:DriverServiceImpl 2019-01-17 14:56:43
 * @project skz  V1.3.3
 * Copyright(c) 2018 Liu,Wenzhao Co. Ltd.
 * All right reserved.
 */
package com.bingsum.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;

import com.bingsum.model.Driver;
import com.bingsum.util.ApiUtil;
import com.bingsum.util.ParaData;
import com.bingsum.annotation.Api;
import com.bingsum.mapper.DriverMapper;
import tk.mybatis.mapper.entity.Example;

/**
 *
 * @Description: [司机信息]——SERVICE
 * @Author: Liu, Wenzhao
 * @CreateDate: 2019-01-17 14:56:43
 * @Version: V1.3.3
 *
 */
@Service("driverService")
@Transactional(readOnly = true)
public class DriverService {

    @Autowired
    private DriverMapper driverMapper;

    @Api
    public Object driverPageList(ParaData pd) {
        //灵活转换成实体类
        Driver driver = pd.toBean(Driver.class);
        if (pd.get("pageNo") != null && pd.get("pageSize") != null) {
            Page<?> page = PageHelper.startPage(pd.getInteger("pageNo"), pd.getInteger("pageSize"));
            driverMapper.selectAll();
            return ApiUtil.returnObject(pd, page);
        }
        return ApiUtil.returnFail(pd, "请检查pageNo, pageSize 参数");
    }

    @Api
    public Object getDriverInfoList(ParaData pd) {
        Page<?> page = PageHelper.startPage(pd.getInteger("currentPage"), 20);
        Example example = new Example(Driver.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("deleteTime");
        driverMapper.selectByExample(example);
        return ApiUtil.returnObject(pd, page);
    }

    @Api
    public Object getDriverInfo(ParaData pd) {
        Example example = new Example(Driver.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("deleteTime");
        criteria.andEqualTo("id", pd.getString("id"));
        Driver driver = driverMapper.selectOneByExample(example);

        if (driver == null){
            return ApiUtil.returnDescFail(pd,"无返回");
        }
        return driver;
    }

    @Api
    @Transactional(readOnly = false)
    public Object newDriverInfo(ParaData pd) {
        System.out.println(pd);
        Driver driver= pd.toBean(Driver.class);
        if (driver.getId() != null) {
            this.driverMapper.updateByPrimaryKeySelective(driver);
        } else {
            this.driverMapper.insert(driver);
            driverMapper.newDriverInfo(pd);
        }
        //只返回成功信号
        return ApiUtil.returnOK(pd,driver);
    }

    @Api
    @Transactional(readOnly = false)
    public Object delDriver(ParaData pd) {
        driverMapper.delDriver(pd);
        Driver driver= pd.toBean(Driver.class);
        return ApiUtil.returnOK(pd,driver.getId());
    }

    @Api
    public List<ParaData> getDriverBriefInfo(ParaData pd) {
        return driverMapper.getDriverBriefInfo(pd);
    }

}